<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Loader;
use think\Request;

class Printer extends Controller
{
    /**
     * 新增打印机
     * */
    public function addPrinter()
    {
        $input = Request()->param();

        $shop_id = $input['shop_id'];
        $printer_name = $input['printer_name'];
        $printer_sn = $input['printer_sn'];
        $printer_key = $input['printer_key'];

        if (empty($shop_id)) apiReturn(2, '门店ID缺失');
        if (empty($printer_name)) apiReturn(2, '请输入打印机名称');
        if (empty($printer_sn)) apiReturn(2, '请输入打印机编号');
        if (empty($printer_key)) apiReturn(2, '请输入打印机密钥');

        Loader::import('printer/Api', EXTEND_PATH);
        $obj = new \Api(C('PRINTER_APPID'), C('PRINTER_APPSECRET'));
        $res = $obj->addPrinter($printer_sn, $printer_key, $printer_name);
        if ($res['errorcode'] == 0) {
            $data = [
                'printer_name' => $printer_name,
                'printer_sn' => $printer_sn,
                'printer_key' => $printer_key,
                'update_time' => time()
            ];
            $result = Db::name('shop')->where(['id' => $shop_id])->update($data);

            if ($result) {
                apiReturn(1, '打印机设置成功');
            } else {
                $this->deletePrinter($printer_sn);
                apiReturn(2, '打印机设置失败');
            }
        } else {
            apiReturn(2, $res['errormsg']);
        }
    }

    public function deletePrinter($printer_sn)
    {
        Loader::import('printer/Api', EXTEND_PATH);
        $obj = new \Api(C('PRINTER_APPID'), C('PRINTER_APPSECRET'));
        return $obj->deletePrinter($printer_sn);
    }

    /**
     * 删除打印机
     * */
    public function delPrinter()
    {
        $input = Request()->param();

        $printer_sn = $input['printer_sn'];

        if (empty($printer_sn)) apiReturn(2, '打印机编号缺失');

        try {
            $data = [
                'printer_name' => '',
                'printer_sn' => '',
                'printer_key' => '',
                'update_time' => time()
            ];
            $db_res = Db::name('shop')->where(['printer_sn' => $printer_sn])->update($data);

            if ($db_res) {
                $res = $this->deletePrinter($printer_sn);
                if ($res['errorcode'] == 0) {
                    Db::commit();
                    apiReturn(1, '删除成功');
                } else {
                    Db::rollback();
                    apiReturn(2, '删除失败');
                }
            } else {
                Db::rollback();
                apiReturn(2, '删除失败');
            }
        } catch (\Exception $e) {
            Db::rollback();
            apiReturn(2, '删除失败');
        }
    }

    /**
     * 打印订单
     * */
    function printOrder($printer_sn, $content)
    {
        Loader::import('printer/Api', EXTEND_PATH);
        $obj = new \Api(C('PRINTER_APPID'), C('PRINTER_APPSECRET'));
        $obj->prints($printer_sn, $content);
    }

}
